//
//  HRBackGroundRemendView.m
//  Hear
//
//  Created by mac on 2017/9/3.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRBackGroundImageRemendView.h"
@interface HRBackGroundImageRemendView()
@property (nonatomic, copy)SelectClickBlock selectClickBlock;
@property(nonatomic, assign)BOOL isCancel;

@end
@implementation HRBackGroundImageRemendView

+(void)showBackGroundImageRemendView:(SelectClickBlock)selectClickBlock andIsCancel:(BOOL)isCancel{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRBackGroundImageRemendView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRBackGroundImageRemendView class]]){
        
        
        HRBackGroundImageRemendView *shareView = (HRBackGroundImageRemendView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:selectClickBlock andIsCancel:isCancel];
        
    }
    
}
- (void)loadView:(SelectClickBlock)selectClickBlock andIsCancel:(BOOL)isCancel
{
    self.isCancel = isCancel;
    _selectClickBlock = selectClickBlock;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeView)];
    [self addGestureRecognizer:tap];
    
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, kFullHeight+120, kFullWidth, 120)];
    showView.backgroundColor = [UIColor whiteColor];
    [self addSubview:showView];
    [UIView animateWithDuration:0.3 animations:^{
        showView.top = kFullHeight - 120;
    }];
    
    UIButton *photoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kFullWidth, showView.height/2)];
    [photoButton setTitle:@"从相册选择一张" forState:UIControlStateNormal];
    [photoButton setTitleColor:FirstTextColor forState:UIControlStateNormal];
    photoButton.titleLabel.font = CustomUIFont(15);
    [photoButton addTarget:self action:@selector(photoButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [showView addSubview:photoButton];
    
    UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(0, photoButton.bottom, photoButton.width, photoButton.height)];
    
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    deleteButton.titleLabel.font = CustomUIFont(15);
    [deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [showView addSubview:deleteButton];
    if (isCancel){
        [deleteButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    
}

- (void)photoButtonClick
{
    [self closeView];
    self.selectClickBlock(0);
}

- (void)deleteButtonClick
{
    [self closeView];
    if (!self.isCancel){
        self.selectClickBlock(1);
    }
}

-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].top = kFullHeight+120;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
